# LoginPage.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from appium.webdriver.common.appiumby import AppiumBy
from selenium.common.exceptions import NoSuchElementException

class LoginPage:
    def __init__(self, driver):
        self.driver = driver

        # 定位器
        if driver.capabilities.get('platformName')=='Android':
            self.shopcart_button = (AppiumBy.XPATH, "//android.widget.TextView[@text='购物车']")
            self.tologin_button = (AppiumBy.XPATH, "//android.widget.TextView[@text='去登陆>']")
            self.login_button = (AppiumBy.XPATH, "//android.widget.TextView[@text='登录']")
            self.to_pay_button = (AppiumBy.XPATH, "//android.widget.TextView[@text='去结算']")
            self.username_input = (AppiumBy.XPATH, "//android.view.View[@resource-id='username']/android.view.View/android.widget.EditText")
            self.password_input = (AppiumBy.XPATH, "//android.view.View[@resource-id='password']/android.view.View/android.widget.EditText")
            # self.locator = (AppiumBy.ID, "com.example.app:id/username")    # 使用 resource-id 定位
            # self.locator = (AppiumBy.ACCESSIBILITY_ID, "Submit")   # 使用 content-desc 定位
            # self.locator = (AppiumBy.CLASS_NAME, "android.widget.Button") # 使用 class name 定位
            # self.locator = (AppiumBy.NAME, "Submit")  # 使用 text 定位
            # self.locator = (AppiumBy.LINK_TEXT, "Learn More")  # 使用链接文本定位
            # self.locator = (AppiumBy.PARTIAL_LINK_TEXT, "Learn") # 使用部分链接文本定位
            # self.locator = (AppiumBy.TAG_NAME, "button") # 使用标签名称定位
            # self.locator = (AppiumBy.CSS_SELECTOR, "button#submit") # 使用 CSS 选择器定位
            # self.locator = (AppiumBy.PREDICATE, "text == 'Submit' AND class == 'android.widget.Button'")  # 使用谓词字符串定位
            # self.locator = (AppiumBy.XPATH, "//android.widget.Button[@text='Submit']")       
        elif driver.capabilities.get('platformName')=='ios':
            self.shopcart_button = (AppiumBy.XPATH, "//XCUIElementTypeButton[@name='购物车']")
            self.tologin_button = (AppiumBy.XPATH, "//XCUIElementTypeButton[contains(@value, '去登陆>')]")
            self.login_button = (AppiumBy.XPATH, "//XCUIElementTypeButton[@name='登录']")
            # self.locator = (AppiumBy.ACCESSIBILITY_ID, "Username")  # 使用 accessibilityId 定位
            # self.locator = (AppiumBy.ID, "com.example.app:id/submit") # 使用 id 定位
            # self.locator = (AppiumBy.CLASS_NAME, "XCUIElementTypeButton") # 使用 class name 定位
            # self.locator = (AppiumBy.NAME, "Welcome") # 使用 name 定位
            # self.locator = (AppiumBy.LABEL, "Welcome") # 使用 label 定位
            # self.locator = (AppiumBy.VALUE, "Enter your username") # 使用 value 定位
            # self.locator = (AppiumBy.PREDICATE, "type == 'XCUIElementTypeButton' AND name == 'Submit'") # 使用 predicate string 定位
            # self.locator = (AppiumBy.CSS_SELECTOR, "XCUIElementTypeButton[name='Submit']") # 使用 css selector 定位
            # self.locator = (AppiumBy.XPATH, "//XCUIElementTypeTextField[@name='Username']")              
        elif driver.capabilities.get('platformName')=='other' :
              print("其他平台")

    def wait_for_element_visible(self, locator, timeout=10):
        # 显示等待直到元素可见
        element = WebDriverWait(self.driver, timeout).until(EC.visibility_of_element_located(locator))
        return element
    
    def wait_for_all_element_visible(self, locator, timeout=10):
        # 显示等待直到元素可见                               
        element = WebDriverWait(self.driver, timeout).until(EC.visibility_of_all_elements_located(locator))
        return element
    
    def navigateToLogin(self):
        # 导航到登录页面
        shopcart_button = self.wait_for_element_visible(self.shopcart_button)
        shopcart_button.click()
        tologin_button = self.wait_for_element_visible(self.tologin_button)
        tologin_button.click()

    # 清除用户名输入框的内容
    def clear_username_input(self):
        username_input = self.wait_for_element_visible(self.username_input)
        username_input.clear()
      
    # 清除密码输入框的内容
    def clear_password_input(self):
        password_input = self.wait_for_element_visible(self.password_input)
        password_input.clear()
        # password_input.send_keys(Keys.CONTROL + "a") 
        # password_input.send_keys(Keys.DELETE)  
      
    # 输入用户名
    def enter_username(self, username):
        username_input = self.wait_for_element_visible(self.username_input)
        username_input.send_keys(username)
    
    # 输入密码
    def enter_password(self, password):
        password_input = self.wait_for_element_visible(self.password_input)
        password_input.send_keys(password)

    # 点击登录按钮
    def click_login_button(self):
        login_button = self.wait_for_element_visible(self.login_button)
        login_button.click()
        
    # 点击购物车按钮
    def click_shopcart_button(self):
        shopcart_button = self.wait_for_element_visible(self.shopcart_button)
        shopcart_button.click()

    # 点击去登录按钮
    def click_tologin_button(self):
        tologin_button = self.wait_for_element_visible(self.tologin_button)
        tologin_button.click()    
    
    # 判断登录按钮是否可见
    def is_login_button_show(self):
        login_button = self.wait_for_element_visible(self.login_button)
        if  login_button.is_displayed():
            return True
        else:
            return False
        
    # 判断支付按钮是否可见
    def is_to_pay_button_show(self):
        to_pay_button = self.wait_for_element_visible(self.to_pay_button)
        if  to_pay_button.is_displayed():
            return True
        else:
            return False
        
    # 检查用户是否已登录
    def is_user_logged_in(self):
        try:
            to_pay_button = self.wait_for_element_visible(self.to_pay_button)
            if  to_pay_button.is_displayed():
                return True
            else:
                return False
        except NoSuchElementException as nsee:
            print(f"找不到元素: {e}")
            return False  # 不继续向上抛出异常
        except Exception as e:
            print(f"未知错误: {e}")
            return False
        
    def get_error_message(self):
        # 获取错误消息
        error_message_element = self.wait_for_element_visible(self.error_message_locator)
        return error_message_element.text
    






